package cn.smbms.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import com.alibaba.druid.pool.DruidDataSourceFactory;

@Configuration //表示该类是一个spring配置类，代替 applicationContext.xml
//@MapperScan(basePackages = "cn.smbms.dao.**") 代替 MapperScannerConfigurer
@ComponentScan(basePackages = {"cn.smbms.service"}) //代替<context:component-scan base-package="cn.smbms.service" />  
@EnableTransactionManagement //代替 <tx:annotation-driven proxy-target-class="true"/>
@Import({CacheConfig.class, ShiroConfig.class})
public class ApplicationConfig {

    @Bean //代替 <bean/>标签，方法名称代替 id，返回类型代替 class
    public DataSource dataSource(){
        DataSource dataSource = null;
        InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("database.properties");
        Properties properties = new Properties();
        try {
            properties.load(inStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }	
    
    @Bean //<bean/>标签中<property/>用 set 方法替代
    public SqlSessionFactoryBean sqlSessionFactory(){
        SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
        bean.setDataSource(dataSource());
        bean.setConfigLocation(new ClassPathResource("mybatis/mybatis-config.xml"));
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/*Mapper.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bean;
    }

    @Bean
    public DataSourceTransactionManager transactionManager(){
        DataSourceTransactionManager ds=new DataSourceTransactionManager();
        ds.setDataSource(dataSource());
        return ds;
    }    
    
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
    	//通用mapper
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("cn.smbms.dao.**");
        return mapperScannerConfigurer;
    }
    
}
